#include "dbustypedlg.hpp"
#include <qdebug.h>
DbusTypeDlg::DbusTypeDlg(QWidget *parent) :
    QDialog(parent)
{
    this->resize(350, 248);
    gridLayout = new QGridLayout(this);
    gridLayout->setObjectName(QStringLiteral("gridLayout"));

    lbType = new QLabel(this);
    lbType->setObjectName(QStringLiteral("lbType"));
    lbType->setText(QString::fromUtf8("类型"));
    gridLayout->addWidget(lbType, 0, 0,1,1);
    cbType = new QComboBox(this);
    cbType->setObjectName(QStringLiteral("cbType"));
    cbType->clear();
    cbType->insertItems(0, QStringList()
     << QString::fromUtf8("SessionBus")
     << QString::fromUtf8("SystemBus")
     << QString::fromUtf8("TCP")
    );
    cbType->setCurrentIndex(2);
    connect(cbType,&QComboBox::currentTextChanged,this,&DbusTypeDlg::on_cbType_currentTextChanged);
//    QObject::connect(cbType,SIGNAL(currentIndexChanged(int)),this,SLOT(on_cbType_currentIndexChanged(int)));
    gridLayout->addWidget(cbType, 0, 1,1,1);

    lbIP = new QLabel(this);
    lbIP->setObjectName(QStringLiteral("lbIP"));
    lbIP->setText(QString::fromUtf8("IP地址"));

    gridLayout->addWidget(lbIP, 1, 0,1,1);

    leIP = new QLineEdit(this);
    leIP->setObjectName(QStringLiteral("leIP"));

    QRegExp regExp("^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){0,3}$");
    leIP->setValidator(new QRegExpValidator(regExp, leIP));
    leIP->setInputMask("000.000.000.000");
    leIP->setText("127.0.0.1");
    leIP->setEnabled(true);
    gridLayout->addWidget(leIP, 1, 1,1,1);

    lbPort = new QLabel(this);
    lbPort->setObjectName(QStringLiteral("lbPort"));
    lbPort->setText(QString::fromUtf8("端口"));

    gridLayout->addWidget(lbPort, 2,0,1,1);

    lePort = new QLineEdit(this);
    lePort->setObjectName(QStringLiteral("lePort"));
    QRegExp regx("[1-9][0-9]{1,5}$");
    lePort->setValidator(new QRegExpValidator(regx, leIP));
//    lePort->setInputMask("000000");
    gridLayout->addWidget(lePort, 2, 1,1,1);
    horizontalSpacer = new QSpacerItem(40, 29, QSizePolicy::Expanding, QSizePolicy::Minimum);
    gridLayout->addItem(horizontalSpacer, 3, 0,1,1);
    lePort->setEnabled(true);
    lePort->setText("12434");


    horizontalLayout = new QHBoxLayout();
    horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
    horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    horizontalLayout->addItem(horizontalSpacer_2);
    btOK = new QPushButton(this);
    btOK->setObjectName(QStringLiteral("btOK"));
    btOK->setText(QString::fromUtf8("确认"));
    connect(btOK,&QPushButton::clicked,this,&DbusTypeDlg::on_btOK_clicked);
    horizontalLayout->addWidget(btOK);
    gridLayout->addItem(horizontalLayout, 3, 1,1,1);

}

DbusTypeDlg::~DbusTypeDlg()
{
}

void DbusTypeDlg::on_cbType_currentTextChanged(const QString & text)
{
//    qDebug()<<"index:"<<text<<cbType->currentIndex();
    if(cbType->currentIndex()==2)
    {
        leIP->setEnabled(true);
        lePort->setEnabled(true);
        leIP->setFocus();
    }else
    {
        leIP->setEnabled(false);
        lePort->setEnabled(false);
    }
}

void DbusTypeDlg::on_cbType_currentIndexChanged(int index)
{
//    qDebug()<<"index:"<<index;
}

void DbusTypeDlg::on_btOK_clicked()
{
    m_type = cbType->currentIndex();
    m_IP = leIP->text();
    m_port = lePort->text();
    return QDialog::accept();
}
